script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "using config file: $script_dir/cap_hi_config-${4}.txt"

# cap_config determines which capture configuration to use
if [ -z "$4" ]; then
    cap_config=usual
else
    cap_config=$4
fi

hdc file send $script_dir/cap_hi_config-$cap_config.txt /data/local/tmp/config.txt

# case_prefix is appended before output file
if [ -z "$1" ]; then
    case_prefix=temp
else
    case_prefix=$1
fi

# experiment_name is the folder name of the output file
if [ -z "$2" ]; then
    experiment_name=temp
else
    experiment_name=$2
fi

# trace_time is number of seconds to capture trace for
if [ -z "$3" ]; then
    trace_time=5
else
    trace_time=$3
fi

hdc shell hiprofiler_cmd \
    -c /data/local/tmp/config.txt \
    -o /data/local/tmp/hiprofiler_data.htrace \
    -t $trace_time \
    -s \
    -k

dst_fdr=~/Desktop/trace/$experiment_name
if [ ! -e "$dst_fdr" ]; then
    mkdir -p $dst_fdr
fi

dst_path=$dst_fdr/${case_prefix}@$(date '+%m-%d_%H-%M-%S').htrace
echo $dst_path

hdc file recv /data/local/tmp/hiprofiler_data.htrace $dst_path
hdc shell rm -f /data/local/tmp/hiprofiler_data.htrace

# ./cap_hi.sh file_name_prefix folder_to_save_to 4 load
# save to folder_to_save_to folder with file_name_prefix as prefix, trace for 4 seconds, load configuration cap_hi_config-load.txt
